﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CIT.WMS.inputShelf
{
    public partial class FrminputShelf : Form
    {
        public FrminputShelf()
        {
            InitializeComponent();
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        BarcodeUtils obj = new BarcodeUtils();

        private void txt_kuwei_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                Utils.ERPType type = new Utils.ERPType();
                if (Utils.CheckConnectERP(out type))
                {
                    if (type == Utils.ERPType.YiFei)
                    {
                        //判断采购订单是否合法
                        Utils.uContext.Accountk__BackingField = "erpinfo";
                        DataTable dt = Utils.NMS.QueryDataTable(Utils.uContext, "select MC003 from INVMC_V where MC003='" + txt_kuwei.Text + "'", new CIT.WMS.MESWcf.CmdParameter[0]);
                        if (dt.Rows.Count == 0)
                        {
                            lab_msg.Text = "库位不合法.";
                            lab_msg.ForeColor = Color.Red;
                            txt_kuwei.SelectAll();
                            txt_kuwei.Focus();
                            return;
                        }
                    }
                }
                else
                {
                    //库位不需要去ERP校验，需要在本地校验，两张库位表

                }
                lab_msg.Text = "库位校验合法.";
                lab_msg.ForeColor = Color.Lime;
                txt_reelid.SelectAll();
                txt_reelid.Focus();
            }
        }

        private void txt_reelid_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                upload();
            }
        }
        void upload()
        {
            DataTable dt = new DataTable();
            Utils.ERPType type = new Utils.ERPType();
            if (Utils.CheckConnectERP(out type))
            {
                if (type == Utils.ERPType.YiFei)
                {
                    //判断库位是否合法
                    Utils.uContext.Accountk__BackingField = "erpinfo";
                    dt = Utils.NMS.QueryDataTable(Utils.uContext, "select MC003 from INVMC_V where MC003='" + txt_kuwei.Text + "'", new CIT.WMS.MESWcf.CmdParameter[0]);
                    if (dt.Rows.Count == 0)
                    {
                        lab_msg.Text = "库位不合法.";
                        lab_msg.ForeColor = Color.Red;
                        txt_kuwei.SelectAll();
                        txt_kuwei.Focus();
                        return;
                    }
                }
            }
            BarcodeUtils.BarObject barobj = obj.AnalysisBarcode(Utils.AnalysisSerPath, txt_reelid.Text);

            Utils.uContext.Accountk__BackingField = "mes40";
            StringBuilder str = new StringBuilder();
            str.Append(string.Format(@"select housecode,wocode,reelid,pocode,partnumber,qty,batch1,isnull(shelfid,'') as shelfid from MdcDatVStorage where ReelID='{0}' and housecode='007' union select housecode,wocode,reelid,pocode,partnumber,qty,batch1,isnull(shelfid,'') as shelfid from MdcDatVStorage where ReelID='{0}' and housecode='001' ", barobj.reelId));
            dt = Utils.NMS.QueryDataTable(Utils.uContext, str.ToString(), new CIT.WMS.MESWcf.CmdParameter[0]);
            if (dt.Rows.Count == 0)
            {
                lab_msg.Text = "料卷非待上架状态.";
                lab_msg.ForeColor = Color.Red;
                txt_reelid.SelectAll();
                txt_reelid.Focus();
                return;
            }
            else
            {
                if (dt.Rows[0]["isenable"] != null && dt.Rows[0]["isenable"].ToString() == "1")
                {
                    lab_msg.Text = "料卷已经禁用";
                    lab_msg.ForeColor = Color.Red;
                    txt_reelid.SelectAll();
                    txt_reelid.Focus();
                    return;
                }
                //判断是否在货架上
                if (dt.Rows[0]["shelfid"] != null && dt.Rows[0]["shelfid"].ToString() != "")
                {
                    lab_msg.Text = "料卷已经上架.";
                    lab_msg.ForeColor = Color.Red;
                    txt_reelid.SelectAll();
                    txt_reelid.Focus();
                    return;
                }
                //获取供应商信息
                string po = dt.Rows[0]["pocode"].ToString();

                //判断IQC检验结果是否可以入库
                string checkReceive = "select status,Recive from MdcDatIQCCheck where po='" + po + "' and pn='" + dt.Rows[0]["partnumber"].ToString() + "' --and lot='" + dt.Rows[0]["batch1"].ToString() + "'";
                DataTable dt_checkReceive = Utils.NMS.QueryDataTable(Utils.uContext, checkReceive, new CIT.WMS.MESWcf.CmdParameter[0]);
                if (dt_checkReceive != null && dt_checkReceive.Rows.Count > 0)
                {
                    if (dt_checkReceive.Rows[0][0].ToString() == "0")
                    {
                        lab_msg.Text = "IQC待检状态.";
                        lab_msg.ForeColor = Color.Red;
                        txt_reelid.SelectAll();
                        txt_reelid.Focus();
                        return;
                    }
                    else if (dt_checkReceive.Rows[0][0].ToString() == "2")
                    {
                        lab_msg.Text = "IQC抽样完成状态.";
                        lab_msg.ForeColor = Color.Red;
                        txt_reelid.SelectAll();
                        txt_reelid.Focus();
                        return;
                    }
                    else if (dt_checkReceive.Rows[0][0].ToString() == "3")
                    {
                        lab_msg.Text = "IQC开始抽样状态.";
                        lab_msg.ForeColor = Color.Red;
                        txt_reelid.SelectAll();
                        txt_reelid.Focus();
                        return;
                    }
                    else if (dt_checkReceive.Rows[0][0].ToString() == "4")
                    {
                        lab_msg.Text = "IQC开始检验状态.";
                        lab_msg.ForeColor = Color.Red;
                        txt_reelid.SelectAll();
                        txt_reelid.Focus();
                        return;
                    }
                    else if (dt_checkReceive.Rows[0][0].ToString() == "1")
                    {
                        if (dt_checkReceive.Rows[0][1].ToString() == "0")
                        {
                            decimal qty = 0;
                            try
                            {
                                qty = Convert.ToDecimal(dt.Rows[0]["qty"].ToString());
                            }
                            catch
                            {

                            }
                            //判断当前采购订单数量是否超额录入


                            //回报ERP系统,先存入缓存区，每隔一段时间同步一次至ERP

                            string sql = "";
                            string supplierNo = "";
                            if (Utils.CheckConnectERP(out type))
                            {
                                if (type == Utils.ERPType.YiFei)
                                {
                                    //先根据po再去获取采购订单信息
                                    Utils.uContext.Accountk__BackingField = "erpinfo";
                                    sql = "select top 1 TC002 from PURTC_V where TC001='" + po + "'";
                                    DataTable dt_supplier = Utils.NMS.QueryDataTable(Utils.uContext, sql, new CIT.WMS.MESWcf.CmdParameter[0]);

                                    if (dt_supplier.Rows.Count > 0)
                                    {
                                        supplierNo = dt.Rows[0]["TC002"].ToString();
                                    }
                                }
                            }
                            bool flag = false;
                            bool result = false;
                            Utils.uContext.Accountk__BackingField = "mes40";
                            sql = string.Format(@"insert into SfcDatERPJHinfo(FGUID,pocode,supplier,housecode,partnumber,sqty,nqty,locationid,status,creator,createtime)
                values(NEWID(),'{0}','{1}','','{2}','{3}','0','{4}','0','{5}',getdate()) ", po, supplierNo, dt.Rows[0]["partnumber"].ToString(), qty, dt.Rows[0]["shelfid"].ToString(), Utils.uContext.UserIDk__BackingField);
                            //Utils.NMS.Exectransql(Utils.uContext, sql, new CIT.WMS.MESWcf.CmdParameter[0], out result, out flag);
                            string housecode = dt.Rows[0]["housecode"].ToString();

                            sql += " update mdcdatvstorage set shelfid='" + txt_kuwei.Text + "',housecode='001',LockStatus='0' where reelid='" + barobj.reelId + "' ";
                            sql += @" if not exists (select * from MdcDatRStorage where PartNumber='" + dt.Rows[0]["partnumber"].ToString() + @"')
                                begin
	                                insert into MdcDatRStorage(PartNumber,Qty)values('" + dt.Rows[0]["partnumber"].ToString() + @"','0')
                                end   
	                                update mdcdatrstorage set qty=qty+" + qty + @" where partnumber='" + dt.Rows[0]["partnumber"].ToString() + @"'";
                            //增加SyssystemLog日志 nancy 2017.07.26
                            sql += " insert SyssystemLog(Fguid,line,WoCode,SfcNo,ProuctCode,PartNumber,ReelID,pocode,SupplierID,HouseCode,Qty,Remarks,Creator) ";
                            sql += " select newid(),line,wocode,sfcno,'',PartNumber,ReelID,pocode,SupplierID,'001',Qty,'PDA物料上架(含账务)', '" + Utils.uContext.UserNamek__BackingField + "' from mdcdatvstorage where reelid='" + barobj.reelId + "'";  
                            Utils.NMS.Exectransql(Utils.uContext, sql, new CIT.WMS.MESWcf.CmdParameter[0], out flag, out result);
                            if (flag)
                            {
                                Utils.SaveAppropriationSheet(new AppropriationSheet() { PartNumber = dt.Rows[0]["partnumber"].ToString(), MType = "7", WOCode = dt.Rows[0]["wocode"].ToString(), POCode = po, ReelID = barobj.reelId, Qty = dt.Rows[0]["qty"].ToString(), Description = "上架[" + txt_kuwei.Text + "]", HouseCode = "001", FromHouse = housecode, User = Utils.uContext.UserNamek__BackingField });
                                lab_msg.Text = "料卷上架成功.";
                                lab_msg.ForeColor = Color.Lime;
                                txt_kuwei.Text = "";
                                txt_reelid.Text = "";
                                txt_kuwei.SelectAll();
                                txt_kuwei.Focus();
                                return;
                            }
                        }
                        else if (dt_checkReceive.Rows[0][1].ToString() == "1")
                        {
                            lab_msg.Text = "IQC检验失败,不可入库.";
                            lab_msg.ForeColor = Color.Red;
                            txt_reelid.SelectAll();
                            txt_reelid.Focus();
                            return;
                        }
                        else if (dt_checkReceive.Rows[0][1].ToString() == "2")
                        {
                            lab_msg.Text = "IQC检验状态未知.";
                            lab_msg.ForeColor = Color.Red;
                            txt_reelid.SelectAll();
                            txt_reelid.Focus();
                            return;
                        }
                    }
                }
                else
                {
                    lab_msg.Text = "未获取到IQC检验记录";
                    lab_msg.ForeColor = Color.Red;
                    txt_reelid.SelectAll();
                    txt_reelid.Focus();
                    return;
                }

            }
        }

        private void btn_yes_Click(object sender, EventArgs e)
        {
            upload();
        }
    }
}